<template>
  <div class="wrap">
    <div class="title">
      <div>
        <img src="../assets/shop.png" class="shopUrl" alt />
        <p>{{name}}</p>
      </div>
      <p>共{{orderList.length}}个商品</p>
    </div>
    <div class="shopList">
      <img v-for="item in orderList" :onerror="defaultImg2" :key="item.id" :src="item.img" alt @click="maxshow(item.img)" />
    </div>
    <div class="item">
      <template v-if="defaultAddress">
        <div class="center" @click="setAddress(1)">
        <img src="../assets/minj.png" class="icon" alt />
        <div class="left">
          <p>{{defaultAddress.name}}：{{defaultAddress.phone}}</p>
          <p>{{defaultAddress.province}}-{{defaultAddress.city}}-{{defaultAddress.district}}</p>
          <p>{{defaultAddress.content}}</p>
        </div>
      </div>
      </template>
      <template v-else>
        <div class="center" @click="setAddress(1)">
          <img src="../assets/mins.png" class="icon" alt />
          <h3>请添加寄件人地址</h3>
        </div>
      </template>
      <div class="ri">
        <img src="../assets/right.png" class="ricon" alt />
        <div class="rights" @click="goList(1)">
          <img src="../assets/order.png" class="address-icon" alt />
          <span>地址簿</span>
        </div>
      </div>
    </div>
    <div class="item">
      <template v-if="defaultAddress2">
        <div class="center"  @click="setAddress(2)">
          <img src="../assets/mins.png" class="icon" alt />
          <div class="left">
            <p>{{defaultAddress2.name}}：{{defaultAddress2.phone}}</p>
            <p>{{defaultAddress2.province}}-{{defaultAddress2.city}}-{{defaultAddress2.district}}</p>
            <p>{{defaultAddress2.content}}</p>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="center" @click="setAddress(2)">
          <img src="../assets/mins.png" class="icon" alt />
          <h3>请添加收件人地址</h3>
        </div>
      </template>
      <div class="ri">
        <img src="../assets/right.png" class="ricon" alt />
        <div class="rights" @click="goList(2)">
          <img src="../assets/order.png" class="address-icon" alt />
          <span>地址簿</span>
        </div>
      </div>
    </div>
    <van-popup v-model="show">
      <img :src="maxImg" class="maxIMg" :onerror="defaultImg2" alt />
    </van-popup>
    <p class="btn" @click="next">提交</p>
  </div>
</template>
<style lang="scss" scoped>
.maxImg{
  width: 100%;
}
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 8.4rem;
  margin: 0.666667rem auto 0;

  div {
    display: flex;
    align-items: center;
    .shopUrl {
      width: 0.533333rem;
      height: 0.533333rem;
      display: block;
    }
    p {
      margin-left: 0.333333rem;
      font-size: 0.4rem;
      color: #555;
    }
  }
  p {
    color: #999;
    font-size: 0.373333rem;
  }
}
.shopList {
  display: flex;
  flex-wrap: wrap;
  padding-bottom: 0.4rem;
  margin: 0.4rem auto 0;
  padding: 0 0.8rem 0.4rem;
  img {
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 0.266667rem;
    margin-right: 0.2rem;
    margin-bottom: 0.2rem;
    display: block;
     object-fit: cover;
  }
}

.item {
  margin: 0 auto 0;
  padding: 0.4rem 0.8rem;
  display: flex;
  align-items: center;
  .icon {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    display: block;
  }
  h3 {
    font-weight: normal;
    text-align: left;
    color: #999;
    padding-left: 0.2rem;
  }
  .center {
    display: flex;
    align-items: center;
    height: 1.2rem;
    width: 7.9rem;
    margin-bottom: 2px;
    .left {
      margin-left: 0.2rem;
      p:nth-of-type(1) {
        color: #555;
      }
      p {
        text-align: left;
        color: #999;
        font-size: 0.32rem;
      }
    }
  }
  .ri {
    display: flex;
    align-items: center;
    .ricon {
      display: block;
      width: 0.5rem;
      height: 0.5rem;
      margin-right: 0.4rem;
    }
    .rights {
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 0.133333rem;
      width: 0.8rem;
      .address-icon {
        width: 0.44rem;
        height: 0.493333rem;
        display: block;
        margin-bottom: 0.133333rem;
      }
      span {
        color: #999;
        font-size: 0.24rem;
      }
    }
  }
}
.btn {
  width: 8.16rem;
  height: 1.066667rem;
  margin: 1rem auto 0;
  background: #3fd9c9;
  color: #fff;
  font-size: 0.48rem;
  text-align: center;
  line-height: 1.066667rem;
  border-radius: 0.266667rem;
}
.wrap {
  padding-bottom: 1rem;
}
// .wrapper {
//   display: flex;
//   align-items: center;
//   justify-content: center;
//   height: 100%;
//   img {
//     width: 80%;
//   }
// }
</style>
<script>
import { Dialog } from "vant";
import { addressList, issuing } from "@/utils/interface";
export default {
  data() {
    return {
      orderList: [],
      maxImg: "",
      show: false,
      defaultAddress: "",
      defaultAddress2: "",
      strIds: [],
      name:"",
      defaultImg2: 'this.src="'+ require('../assets/timg.jpg') +'"',
    };
  },
  created() {
    document.title = "填写地址";
    
    this.orderList = JSON.parse(this.$route.query.orderList);
    this.name = this.$route.query.name
    console.log(this.orderList)
    this.orderList.map(item => {
      this.strIds.push(item.id);
    });
    if(this.$store.state.address1){
      this.defaultAddress = this.$store.state.address1
    }else{
      this.getAddress();
    }
    if(this.$store.state.address2){
      this.defaultAddress2 = this.$store.state.address2
    }

  },
  methods: {
    maxshow(item) {
      this.show = true;
      this.maxImg = item;
    },
    next() {
      issuing({
        deliveryAddressId: this.defaultAddress.address_id,
        takeAddressId: this.defaultAddress2.address_id,
        orderList: this.strIds.toString()
      }).then(res => {
        if (res.data.code == 200) {
          Dialog.alert({
            title: "提示",
            message: "提交成功"
          }).then(() => {
            this.$router.replace("/");
            this.$store.commit('clearAddress')
          });
        }
      });
    },
    goList(t) {
      this.$router.push({path:'selectUser',query:{name:1,type:t}});
    },
    setAddress(n) {
      this.$router.push({path:'s_address',query:{id:n}});
    },
    // 获取默认地址
    getAddress() {
      addressList({ isDefault: 1 }).then(res => {
        if(res.data.code == 200&&res.data.data.length){
          this.defaultAddress = res.data.data[0];
        this.defaultAddress.phone =
          this.defaultAddress.tel.substr(0, 3) +
          "****" +
          this.defaultAddress.tel.substr(7);
        }
        
      });
    }
  }
};
</script>